layui.use(['form','layer','jquery'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer;
        $ = layui.jquery;

    //快速登陆
    $.get("/user/login",function (res) {
        if(res.code) {
            layer.msg("登陆成功，正在跳转！",{icon:1,time:800,shade:[0.6,'#000',true]});
            window.location.href = "/page/mobile.html";
        }
    });

    var aCode = [];
    $(function(){
        draw();

        $("#canvas").on('click',function(){
            draw();
        });
    });

    function draw() {
        $.ajax({
            type:"GET",
            url:'/user/imgCode',
            async:false,
            success:function(res){
                aCode = res;
            }
        });

        var canvas_width=$('#canvas').width();
        var canvas_height=$('#canvas').height();
        var canvas = document.getElementById("canvas");//获取到canvas的对象，演员
        var context = canvas.getContext("2d");//获取到canvas画图的环境，演员表演的舞台
        canvas.width = canvas_width + 1;
        canvas.height = canvas_height + 1;

        for (var i = 0; i <= 3; i++) {
            // var j = Math.floor(Math.random() * aLength);//获取到随机的索引值
            var deg = Math.random() * 30 * Math.PI / 180;//产生0~30之间的随机弧度
            // var txt = aCode[j];//得到随机的一个内容
            var txt = aCode[i];//得到随机的一个内容
            // show_num[i] = txt.toLowerCase();
            var x = 10 + i * 20;//文字在canvas上的x坐标
            var y = 20 + Math.random() * 8;//文字在canvas上的y坐标
            context.font = "bold 23px 微软雅黑";

            context.translate(x, y);
            context.rotate(deg);

            context.fillStyle = randomColor();
            context.fillText(txt, 0, 0);

            context.rotate(-deg);
            context.translate(-x, -y);
        }
        for (var i = 0; i <= 5; i++) { //验证码上显示线条
            context.strokeStyle = randomColor();
            context.beginPath();
            context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
            context.stroke();
        }
        for (var i = 0; i <= 30; i++) { //验证码上显示小点
            context.strokeStyle = randomColor();
            context.beginPath();
            var x = Math.random() * canvas_width;
            var y = Math.random() * canvas_height;
            context.moveTo(x, y);
            context.lineTo(x + 1, y + 1);
            context.stroke();
        }
    }

    function randomColor() {//得到随机的颜色值
        var r = Math.floor(Math.random() * 256);
        var g = Math.floor(Math.random() * 256);
        var b = Math.floor(Math.random() * 256);
        return "rgb(" + r + "," + g + "," + b + ")";
    }

    $(".loginBody .seraph").click(function(){
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧",{
            time:5000
        });
    });

    //登录按钮
    form.on("submit(login)",function(data){

        var username = $("#userName").val().trim();
        var password = $("#password").val().trim();
        var code = $("#code").val().trim();
        if(username.indexOf("@") != -1) {
            if(!(/^([a-zA-Z\d])(\w|\-)+@[a-zA-Z\d]+\.[a-zA-Z]{2,4}$/.test(username))) {
                layer.msg('邮箱格式错误！',{icon:7,time:800,shade:[0.6,'#000',true]});
                return false;
            }
        }else {
            if(username.length != 10) {
                layer.msg('账号错误！',{icon:7,time:800,shade:[0.6,'#000',true]});
                return false;
            }
        }


        if(code.length != 4) {
            layer.msg("验证码长度错误，请重填！",{icon:7,time:800,shade:[0.6,'#000',true]});
            return false;
        }

        for(var i = 0;i < 4;i++) {
            if(code.charAt(i) != aCode[i]) {
                layer.msg("验证码错误，请重填！",{icon:7,time:800,shade:[0.6,'#000',true]});
                return false;
            }
        }

        layer.msg('正在登陆，请稍后！',{icon:1,time:800,shade:[0.6,'#000',true]});
        setTimeout(function(){
            //4、对密码进行加密
            var md5password = hex_md5(password);

            $.ajax({
                type:"POST",
                url:'/user/login',
                dataType: "json",
                data: { username: username, password: md5password,code: code },
                async:false,
                success:function(res){
                    if(res.code) {
                        layer.msg("登陆成功，正在跳转！",{icon:1,time:800,shade:[0.6,'#000',true]});
                        window.location.href = "/page/mobile.html";
                    }else {
                        layer.msg(res.msg,{icon:7,time:800,shade:[0.6,'#000',true]});
                    }
                }
            });

        },1000);
        return false;
    });

    //表单输入效果
    $(".loginBody .input-item").click(function(e){
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    });
    $(".loginBody .layui-form-item .layui-input").focus(function(){
        $(this).parent().addClass("layui-input-focus");
    });
    $(".loginBody .layui-form-item .layui-input").blur(function(){
        $(this).parent().removeClass("layui-input-focus");
        if($(this).val() != ''){
            $(this).parent().addClass("layui-input-active");
        }else{
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
